package com.kangtu.demologinweimeng.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;

/**
 * @program: demo-weix-pay
 * @description: WeiMengController
 * @author: 亮子说编程
 * @create: 2021-03-12 11:48
 **/
@RestController
@Slf4j
public class WeiMengController {

    @Autowired
    private RestTemplate restTemplate;

    private final String client_id = "77F21AC63CB957C56A8C265DAC04765D";
    private final String client_secret = "7B55F64C793F6E5F1DE717BD98A940AA";
    private final String redirect_uri = "http://www.shenmazong.com:9090/wmToken";

    @GetMapping(value = "/getWmToken")
    public Object getWmToken() throws JsonProcessingException {
        // https://dopen.weimob.com/fuwu/b/oauth2/token?grant_type=client_credentials&client_id={client_id}&client_secret={client_secret}
        // https://dopen.weimob.com/fuwu/b/oauth2/authorize?enter=wm&view=pc&response_type=code&scope=default&client_id={client_id}&redirect_uri={http://xxx}&state={state}
        String url = String.format("https://dopen.weimob.com/fuwu/b/oauth2/token?grant_type=client_credentials&client_id=%s&client_secret=%s",
                client_id, client_secret);
        log.info(url);


        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        ObjectMapper mapper = new ObjectMapper();
        HashMap<String, Object> map = new HashMap<>();
        map.put("grant_type", "grant_type");
        map.put("client_id", client_id);
        map.put("client_secret", client_secret);
        String params = mapper.writeValueAsString(map);

        HttpEntity<String> request = new HttpEntity<String>(params, headers);
        String res = restTemplate.postForObject(url, request, String.class);

        return res;
    }

    @RequestMapping(value = "/wmToken")
    public Object putToken(String code, String state) {
        log.info("code="+code);
        log.info("state="+state);
        return "ok";
    }

    @GetMapping(value = "/getToken")
    public Object getToken() throws JsonProcessingException {
        // https://dopen.weimob.com/fuwu/b/oauth2/authorize?enter=wm&view=pc&response_type=code&scope=default&client_id={client_id}&redirect_uri={http://xxx}&state={state}
        String url = String.format("https://dopen.weimob.com/fuwu/b/oauth2/authorize?enter=wm&view=pc&response_type=code&scope=default&client_id=%s&redirect_uri=%s&state=%d",
                client_id, redirect_uri, 1234);
        log.info(url);


        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        ObjectMapper mapper = new ObjectMapper();
        HashMap<String, Object> map = new HashMap<>();
        map.put("grant_type", "grant_type");
        map.put("client_id", client_id);
        map.put("client_secret", client_secret);
        String params = mapper.writeValueAsString(map);

        HttpEntity<String> request = new HttpEntity<String>(params, headers);
        String res = restTemplate.postForObject(url, request, String.class);

        return res;
    }
}
